package com.yt.system.service.impl;

import com.yt.system.dao.VacateMapper;
import com.yt.system.domain.Vacate;
import com.yt.system.service.VacateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VacateServiceImpl implements VacateService {

    @Autowired
    VacateMapper vacateMapper;

    /**
     * 提交请假申请
     * @param vacate
     * @return
     */
    @Override
    public int insertLeave(Vacate vacate) {
        vacate.setVstatus(0);
        vacate.setVflag(0);
        System.out.println("销请假操作");
        return vacateMapper.insertSelective(vacate);
    }

    /**
     * 提交续假申请
     * @param vacate
     * @return
     */
    @Override
    public int insertExtend(Vacate vacate) {
        vacate.setVstatus(0);
        vacate.setVflag(1);
        System.out.println("续假操作");
        return vacateMapper.insertSelective(vacate);
    }

    /**
     * 销假
     * @param vacate
     * @return
     */
    @Override
    public int sellingOffLeave(Vacate vacate) {
        if (vacateMapper.selectByPrimaryKey(vacate.getVid()) != null){
            vacate.setVflag(2);
            System.out.println("销假操作");
            return vacateMapper.updateByPrimaryKeySelective(vacate);
        } else {
            return 0;
        }
    }

    /**
     * 根据uid、vstatus、vflag查询请假记录
     * @param vacate
     * @return
     */
    @Override
    public List<Vacate> findAllVacate(Vacate vacate) {
        if (vacate.getUid() != null){
            return vacateMapper.selectByUid(vacate);
        }else {
            return null;
        }
    }

    /**
     * 根据vstatus、vflag查询请假记录
     * @param vacate
     * @return
     */
    @Override
    public List<Vacate> findAllByVacate(Vacate vacate) {
        if (vacate.getVstatus() != null || vacate.getVflag() != null){
            return vacateMapper.selectAllByVacate(vacate);
        }else {
            return null;
        }
    }

    /**
     * 根据用户名name查询请假记录
     * @param name
     * @return
     */
    @Override
    public List<Vacate> findAllByName(String name) {
        if (name != null) {
            return vacateMapper.selectVacateByName(name);
        }else {
            return null;
        }
    }

    /**
     * 管理员插入请假记录
     * @param vacate
     * @return
     */
    @Override
    public int insertVacate(Vacate vacate) {
        if (vacate != null){
            return vacateMapper.insertSelective(vacate);
        }else {
            return 0;
        }
    }

    /**
     * 管理员更新请假记录
     * @param vacate
     * @return
     */
    @Override
    public int updateVacate(Vacate vacate) {
        if (vacate != null){
            return vacateMapper.updateByPrimaryKeySelective(vacate);
        }else {
            return 0;
        }
    }

    /**
     * 管理员删除请假记录
     * @param vid
     * @return
     */
    @Override
    public int deleteVacate(Integer vid) {
        if (vid != null) {
            return vacateMapper.deleteByPrimaryKey(vid);
        } else {
            return 0;
        }
    }

}
